# Maintainer: Patrick Stewart <patstew@gmail.com>

_realname=jsonschema
pkgbase=mingw-w64-python-${_realname}
pkgname=("${MINGW_PACKAGE_PREFIX}-python2-${_realname}" "${MINGW_PACKAGE_PREFIX}-python3-${_realname}")
pkgver=3.1.1
pkgrel=1
pkgdesc="An implementation of JSON Schema validation for Python (mingw-w64)"
arch=('any')
url="https://pypi.python.org/pypi/jsonschema"
license=('MIT')
makedepends=("${MINGW_PACKAGE_PREFIX}-python2-attrs"
             "${MINGW_PACKAGE_PREFIX}-python3-attrs"
             "${MINGW_PACKAGE_PREFIX}-python2-setuptools"
             "${MINGW_PACKAGE_PREFIX}-python3-setuptools"
             "${MINGW_PACKAGE_PREFIX}-python2-pyrsistent"
             "${MINGW_PACKAGE_PREFIX}-python3-pyrsistent"
             "${MINGW_PACKAGE_PREFIX}-python2-setuptools-scm"
             "${MINGW_PACKAGE_PREFIX}-python3-setuptools-scm"
             "${MINGW_PACKAGE_PREFIX}-python2-functools32")
_dtoken="58/b9/171dbb07e18c6346090a37f03c7e74410a1a56123f847efed59af260a298"
source=("${_realname}-${pkgver}.tar.gz::https://github.com/Julian/jsonschema/archive/v${pkgver}.tar.gz")
sha256sums=('fd095561aa6bc6686c92ef767095f1f0f6e7adb16ed566e8062dfe111a5108b3')

export SETUPTOOLS_SCM_PRETEND_VERSION=${pkgver}

all_build() {
  cd "${srcdir}/${_realname}-${pkgver}"

  MSYS2_ARG_CONV_EXCL="--prefix=;--install-scripts=;--install-platlib=" \
  ${MINGW_PREFIX}/bin/${1} setup.py install \
    --prefix=${MINGW_PREFIX} --root=${pkgdir} --optimize=1

  find "${pkgdir}/" -name "*.pyc" -delete
  find "${pkgdir}/" -type d -empty -delete
}

package_python3-jsonschema() {
  local interpreter=python3
  depends=("${MINGW_PACKAGE_PREFIX}-python3"
           "${MINGW_PACKAGE_PREFIX}-python3-setuptools"
           "${MINGW_PACKAGE_PREFIX}-python3-attrs"
           "${MINGW_PACKAGE_PREFIX}-python3-pyrsistent"
          )
  install=${_realname}3-${CARCH}.install

  all_build ${interpreter}

  install -D -m644 json/LICENSE ${pkgdir}${MINGW_PREFIX}/share/licenses/python3-${_realname}/LICENSE
  
  local _mingw_prefix=$(cygpath -am ${MINGW_PREFIX})
  for _ff in ${pkgdir}${MINGW_PREFIX}/bin/*.py; do
    sed -e "s|${_mingw_prefix}|${MINGW_PREFIX}|g" -i ${_ff}
  done
}

package_python2-jsonschema() {
  local interpreter=python2
  depends=("${MINGW_PACKAGE_PREFIX}-python2"
           "${MINGW_PACKAGE_PREFIX}-python2-setuptools"
           "${MINGW_PACKAGE_PREFIX}-python2-attrs"
           "${MINGW_PACKAGE_PREFIX}-python2-pyrsistent"
           "${MINGW_PACKAGE_PREFIX}-python2-functools32")
  install=${_realname}2-${CARCH}.install

  all_build ${interpreter}

  install -D -m644 json/LICENSE ${pkgdir}${MINGW_PREFIX}/share/licenses/python2-${_realname}/LICENSE

# This entire section should be removed if the package does NOT install
# anything in the /mingw*/bin directory. 
### begin section ###
  local PREFIX_WIN=$(cygpath -wm ${MINGW_PREFIX})
  # fix python command in files
  for _f in "${pkgdir}${MINGW_PREFIX}"/bin/*.py; do
    sed -e "s|/usr/bin/env |${MINGW_PREFIX}|g" \
        -e "s|${PREFIX_WIN}|${MINGW_PREFIX}|g" -i ${_f}
  done

# for Python2 packages, you want to rename some stuff from the bin directory 
# with the 2 suffix like in the mingw-w64-python-pygments package to avoid
# conflicts when installing both the Python2 and Python3 packages
  for f in jsonschema; do
    mv "${pkgdir}${MINGW_PREFIX}"/bin/${f}{,2}.exe
    if [ -f "${pkgdir}${MINGW_PREFIX}"/bin/${f}.exe.manifest ]; then
      mv "${pkgdir}${MINGW_PREFIX}"/bin/${f}{,2}.exe.manifest
      sed -e "s|${f}|${f}2|g" -i "${pkgdir}${MINGW_PREFIX}"/bin/${f}2.exe.manifest
    fi
    mv "${pkgdir}${MINGW_PREFIX}"/bin/${f}{,2}-script.py
  done
#### end section ####

}

package_mingw-w64-i686-python2-jsonschema() {
  package_python2-jsonschema
}

package_mingw-w64-i686-python3-jsonschema() {
  package_python3-jsonschema
}

package_mingw-w64-x86_64-python2-jsonschema() {
  package_python2-jsonschema
}

package_mingw-w64-x86_64-python3-jsonschema() {
  package_python3-jsonschema
}
